import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Params } from "@angular/router";
import { Observable } from "rxjs/Observable";

import { TableTab } from "../f10-shared/interface-shared";
import { scroll } from "../f10-shared/common-methods";
import { removeHMS, compare } from "../f10-shared/util";
import { TableHeader } from "../f10-shared/interface-shared";
import { F10Service } from "../service/f10.service";
import { ScrollService } from "../service/scroll.service";

@Component({
  templateUrl: 'managers.component.html',
  styleUrls: ['managers.component.less']
})

export class ManagersComponent implements OnInit {
  timer = null;
  tabClicked = {
    flag: false
  };
  activeTabIndex = 0;
  scrollTimer = null;
  mustChange = false;
  subLists: TableTab[] = [
    {
      label: "高管简介",
      class: 'sub-title-0'
    },
    {
      label: "高管持股变动",
      class: 'sub-title-1'
    },
    {
      label: "离职高管",
      class: 'sub-title-2'
    }
  ]
  leadersInHeader: TableHeader[] = [
    {
      label: '序号',
      key: '$id'
    },
    {
      label: '姓名',
      key: 'leaderName',
      mouseover: true
    },
    {
      label: '性别',
      key: 'leaderGender'
    },
    {
      label: '出生年',
      key: 'birthYM'
    },
    {
      label: '学历',
      key: 'leaderDegree'
    },
    {
      label: '职位',
      key: 'position'
    },
    {
      label: '任职起始日期',
      key: 'inDate'
    }
  ]
  leadersInSource: Observable<any>;
  leadersOutHeader: TableHeader[] = [
    {
      label: '姓名',
      key: 'leaderName'
    },
    {
      label: '性别',
      key: 'leaderGender'
    },
    {
      label: '出生年',
      key: 'birthYM'
    },
    {
      label: '学历',
      key: 'leaderDegree'
    },
    {
      label: '职位',
      key: 'position'
    },
    {
      label: '任职起始日期',
      key: 'inDate'
    },
    {
      label: '离任日期',
      key: 'offDate'
    }
  ]
  leadersOutSource: Observable<any>;
  // 高管持股变动
  leaderstockaltersHead: TableHeader[] = [
    {
      label: '变动日期',
      key: 'alternationDate'
    },
    {
      label: '变动人',
      key: 'stockHolder'
    },
    {
      label: '变动数量(股)',
      key: 'stockSumChanging'
    },
    {
      label: '变动后持股数（股）',
      key: 'holdSumAfterAlter'
    },
    {
      label: '交易均价（元）',
      key: 'avgPrice'
    },
    {
      label: '职务',
      key: 'positionDesc'
    },
    {
      label: '与高管关系',
      key: 'connectionDesc'
    },
    {
      label: '变动原因',
      key: 'alternationReasonDesc'
    }
  ]
  leaderstockaltersSource: Observable<any>;
  
  constructor (
    private f10Service: F10Service,
    private router: ActivatedRoute,
    private scrollService: ScrollService
  ) {}

  ngOnInit () {
    let symbolObservable = this.router.parent.parent.params.map(params => params["id"]).filter(id => !!id).map(id => {
      return {
        symbol: (<string>id).replace("_", ".")
      };
    })
    // 公司高管
    let allLeaders = symbolObservable
      .switchMap((stock) => this.f10Service.getLeadersin(stock.symbol))
      .map(data => data.map((d, i) => ({
        ...d,
        birthYM: this.justFeedBackYear(d.birthYM),
        inDate: removeHMS(d.inDate),
        offDate: removeHMS(d.offDate)
      }))).publish().refCount();
    let ifIn = 1
    let ifOut = 0
    this.leadersInSource = allLeaders
      .map(data => data.filter(d => d.ifIn === ifIn))
      .map(data => data.map((d, i) => ({
        ...d,
        $id: i + 1,
      })))

    // 离职高管
    this.leadersOutSource = allLeaders
      .map(data => data.filter(d => d.ifIn === ifOut).sort(compare('offDate', 'des')))

    // 高管持股变动
    this.leaderstockaltersSource = symbolObservable
      .switchMap((stock) => this.f10Service.getLeaderstockalters(stock.symbol))
      .map(data => data.map((d, i) => ({
        ...d,
        alternationDate: removeHMS(d.alternationDate)
      })).sort(compare('alternationDate', 'des')))
  }

  tabToggle (event) {
    this.tabClicked.flag = true
    this.timer = scroll(`segment-container`, event, this.timer, this.tabClicked);
  }

  scrollEvent (e, el, el1, el2, el3, el4, el5) {
    if (this.scrollTimer) clearTimeout(this.scrollTimer)
    this.scrollTimer = setTimeout(() => {
      this.mustChange = !this.mustChange
      this.tabClicked.flag = false
      this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
    }, 50)
    if (this.tabClicked.flag) return
    this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
  }

  justFeedBackYear (time) {
    if (time === undefined || time === null || time.toString() === '') return
    return time.slice(0, time.indexOf('-'))
  }
}